package cn.iocoder.yudao.module.crossborder.product.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.crossborder.product.api.dto.BrandDTO;
import cn.iocoder.yudao.module.crossborder.product.api.dto.CategoryDTO;
import cn.iocoder.yudao.module.crossborder.product.api.dto.ProductCreateReqDTO;
import cn.iocoder.yudao.module.crossborder.product.api.dto.ProductDTO;
import cn.iocoder.yudao.module.crossborder.product.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 商品管理
 *
 * @author LHL
 */
@Tag(name = "管理后台 - 商品管理")
@RestController
@RequestMapping("/crossborder/product")
@Validated
public class ProductController {

    @Resource
    private ProductService productService;

    @GetMapping("/get")
    @Operation(summary = "获得商品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:product:query')")
    public CommonResult<ProductDTO> getProduct(@RequestParam("id") Long id) {
        ProductDTO product = productService.getProduct(id);
        return success(product);
    }

    @GetMapping("/list")
    @Operation(summary = "获得商品列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('crossborder:product:query')")
    public CommonResult<List<ProductDTO>> getProductList(@RequestParam("ids") List<Long> ids) {
        List<ProductDTO> products = productService.getProductList(ids);
        return success(products);
    }

    @PostMapping("/create")
    @Operation(summary = "创建商品")
    @PreAuthorize("@ss.hasPermission('crossborder:product:create')")
    public CommonResult<Long> createProduct(@RequestBody @Valid ProductCreateReqDTO createReq) {
        Long id = productService.createProduct(createReq);
        return success(id);
    }

    @GetMapping("/category/get")
    @Operation(summary = "获得商品分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:product:query')")
    public CommonResult<CategoryDTO> getCategory(@RequestParam("id") Long id) {
        CategoryDTO category = productService.getCategory(id);
        return success(category);
    }

    @GetMapping("/category/list")
    @Operation(summary = "获得商品分类列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('crossborder:product:query')")
    public CommonResult<List<CategoryDTO>> getCategoryList(@RequestParam("ids") List<Long> ids) {
        List<CategoryDTO> categories = productService.getCategoryList(ids);
        return success(categories);
    }

    @GetMapping("/brand/get")
    @Operation(summary = "获得商品品牌")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:product:query')")
    public CommonResult<BrandDTO> getBrand(@RequestParam("id") Long id) {
        BrandDTO brand = productService.getBrand(id);
        return success(brand);
    }

    @GetMapping("/brand/list")
    @Operation(summary = "获得商品品牌列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('crossborder:product:query')")
    public CommonResult<List<BrandDTO>> getBrandList(@RequestParam("ids") List<Long> ids) {
        List<BrandDTO> brands = productService.getBrandList(ids);
        return success(brands);
    }

    @PostMapping("/update-stock")
    @Operation(summary = "更新商品库存")
    @Parameter(name = "productId", description = "商品编号", required = true, example = "1024")
    @Parameter(name = "quantity", description = "库存数量", required = true, example = "100")
    @PreAuthorize("@ss.hasPermission('crossborder:product:update')")
    public CommonResult<Boolean> updateStock(@RequestParam("productId") Long productId,
                                            @RequestParam("quantity") Integer quantity) {
        Boolean result = productService.updateStock(productId, quantity);
        return success(result);
    }

    @PostMapping("/update-price")
    @Operation(summary = "更新商品价格")
    @Parameter(name = "productId", description = "商品编号", required = true, example = "1024")
    @Parameter(name = "price", description = "商品价格", required = true, example = "99.99")
    @Parameter(name = "currency", description = "货币代码", required = true, example = "USD")
    @PreAuthorize("@ss.hasPermission('crossborder:product:update')")
    public CommonResult<Boolean> updatePrice(@RequestParam("productId") Long productId,
                                            @RequestParam("price") String price,
                                            @RequestParam("currency") String currency) {
        Boolean result = productService.updatePrice(productId, price, currency);
        return success(result);
    }

}
